﻿/* Copyright © 2012 Gargol, Gargol at gbot dot org
   Based on CoreDC Copyright 2007 © Johan Pettersson, Fredrik Johansson, Michael Starberg, Christian Epstein.
 
This file is part of ADCCore.

ADCCore is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

ADCCore is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ADCCore.  If not, see <http://www.gnu.org/licenses/>. */
using System;
using System.Collections.Generic;
using System.Text;

namespace ADCCore.Classes
{
    public class SearchInfo
    {
        /*
         * public enum SearchDataType { Any = 1, Audio = 2, Compressed = 3, Documents = 4, Executables = 5, Pictures = 6, Video = 7, Folder = 8, TTH = 9 };
TY missing = Any
Any =		BSCH 7ODA TO1570881043 ANtestbot ANsearching ANsorry ANany
Audio = 	FSCH 7ODA -SEGA TO2448387176 ANtestbot ANsearching ANsorry ANaudio EXape EXflac EXm4a EXmid EXmp3 EXmpc EXogg EXra EXwav EXwma
Compressed =  	FSCH 7ODA -SEGA TO3252002573 ANtestbot ANsearching ANsorry ANcompressed EX7z EXace EXarj EXbz2 EXgz EXlha EXlzh EXrar EXtar EXz EXzip
Documents = 	FSCH 7ODA -SEGA TO9419664370 ANtestbot ANsearching ANsorry ANdoc EXdoc EXdocx EXhtm EXhtml EXnfo EXodf EXodp EXods EXodt EXpdf EXppt EXpptx EXrtf EXtxt EXxls EXxlsx EXxml EXxps
Executable = 	FSCH 7ODA -SEGA TO3484345475 ANtestbot ANsearching ANsorry ANexe EXapp EXbat EXcmd EXcom EXdll EXexe EXjar EXmsi EXps1 EXvbs EXwsf
Picture =	FSCH 7ODA -SEGA TO2877952590 ANtestbot ANsearching ANsorry ANpicture EXbmp EXcdr EXeps EXgif EXico EXimg EXjpeg EXjpg EXpng EXps EXpsd EXsfw EXtga EXtif EXwebp
Video = 	FSCH 7ODA -SEGA TO2688005958 ANtestbot ANsearching ANsorry ANvideo EX3gp EXasf EXasx EXavi EXdivx EXflv EXmkv EXmov EXmp4 EXmpeg EXmpg EXogm EXpxp EXqt EXrm EXrmvb EXswf EXvob EXwebm EXwmv
Folder =	BSCH 7ODA TO1552369725 ANtestbot ANsearching ANsorry ANfolder TY2
*/
        public enum SearchDataType { Any = 0, Files = 1,Folder = 2};
        public enum SearchSizeRestriction { LessThen = 0, GreaterThen = 1, EqualTo = 2 };
        /*public enum SearchExtension { Audio = "APE, FLAC, M4A, MID, MP3, MPC, OGG, RA, WAV, WMA", 
            Compressed = "7Z, ACE, ARJ, BZ2, GZ, LHA, LZH, RAR, TAR, TZ, Z, ZIP", 
            Document = "DOC, DOCX, HTM, HTML, NFO, ODF, ODP, ODS, ODT, PDF, PPT, PPTX, RTF, TXT, XLS, XLSX, XML, XPS",
            Executable = "APP, BAT, CMD, COM, DLL, EXE, JAR, MSI, PS1, VBS, WSF",
            Picture ="BMP, CDR, EPS, GIF, ICO, IMG, JPEG, JPG, PNG, PS, PSD, SFW, TGA, TIF, WEBP",
            Video = "3GP, ASF, ASX, AVI, DIVX, FLV, MKV, MOV, MP4, MPEG, MPG, OGM, PXP, QT, RM, RMVB, SWF, VOB, WEBM, WMV"};*/
        public enum SearchExtensionGroups { None = 0, Audio = 1, Compressed = 2, Document = 4, Executable = 8, Picture = 16, Video = 32};
        public SearchInfo()
        {
            this.IsActive = false;
            this.IsTTH = false;
            this.User = new User();
            this.SearchPattern = String.Empty;
            this.DataType = SearchDataType.Any;
            this.SizeRestriction = SearchSizeRestriction.GreaterThen;
            this.SearchSize = 0;
            this.SearchExtensions = String.Empty;
            this.Token = String.Empty;
        }

        /// <summary>
        /// Token must be re-sent to originator
        /// </summary>
        public string Token { get; set; }
        /// <summary>
        /// Signals whether this is an active search or passive
        /// </summary>
        public bool IsActive { get; set; }

        /// <summary>
        /// Signals whether this is a Tiger Tree Hash search or regular word search
        /// </summary>
        public bool IsTTH { get; set; }

        /// <summary>
        /// The user the search originates from
        /// </summary>
        public User User { get; set; }

        /// <summary>
        /// The words or TTH the user is searching for
        /// </summary>
        public string SearchPattern { get; set; }

        /// <summary>
        /// The file extensions the user is searching for
        /// </summary>
        public string SearchExtensions { get; set; }
        /// <summary>
        /// The datatype of the search (what extensions the datatypes map to is client dependent)
        /// </summary>
        public SearchDataType DataType { get; set; }

        /// <summary>
        /// Dependent on SearchSize, signals if it should be a maximum or minimum size
        /// </summary>
        public SearchSizeRestriction SizeRestriction { get; set; }

        /// <summary>
        /// The maximum or minimum (dependent on what SizeRestriction is set to) size of the file(s) to find
        /// </summary>
        public Int64 SearchSize { get; set; }

        /// <summary>
        /// Formats the variables into a proper DC-protocol search
        /// </summary>
        public override string ToString()
        {
            String sp = DcClient.DcEncode(SearchPattern);
            StringBuilder searchInfo = new StringBuilder();
            searchInfo.Append("SCH TY" + DataType);
            if (SearchSize > 0)
            {
                // Yes there is a sizelimit
                if (SizeRestriction == SearchSizeRestriction.GreaterThen)
                    searchInfo.Append(" GE");
                else if (SizeRestriction == SearchSizeRestriction.LessThen)
                    searchInfo.Append(" LE");
                else
                    searchInfo.Append(" EQ");
                searchInfo.Append(SearchSize);
            }
            
            // Add the type of file we want
            //searchInfo.Append(((int)DataType).ToString() + "?");

            // Finally add the searchpattern
            if (IsTTH)
            {
                searchInfo.Append(" TR");
            }
            else
            {
                //search ANDdata
                searchInfo.Append(" AN");
            }
            searchInfo.Append(SearchPattern);
            searchInfo.Append(" TOsch1");
            return searchInfo.ToString();
        }

    }
}
